package com.example.administrator.funchat.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.funchat.R;
import com.xys.libzxing.zxing.activity.CaptureActivity;
import com.xys.libzxing.zxing.encoding.EncodingUtils;


/**
 * Created by Administrator on 2016/7/3.
 */
public class ErweimaDialog extends Activity implements View.OnClickListener {

    public static Bitmap bitmap;
    private Button sao;
    private Button make;
    private Button okButton;
    private EditText mInput;
    private ImageView erweima;
    private CheckBox mLogo;
    private TextView resultText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.erweima_dialog);
        sao = (Button) findViewById(R.id.sao);
        make = (Button) findViewById(R.id.make);
        mInput = (EditText) findViewById(R.id.input);
        resultText = (TextView) findViewById(R.id.result);
        erweima = (ImageView) findViewById(R.id.erweima_change);
        mLogo = (CheckBox) findViewById(R.id.cd_log);
        okButton = (Button) findViewById(R.id.ok02);
        okButton.setOnClickListener(this);
        sao.setOnClickListener(this);
        make.setOnClickListener(this);
    }

    //重写返回键
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {

            Bundle bundle02 = new Bundle();
            Intent intent02 = new Intent();
            bundle02.putString("erweimaImage", "");
            bundle02.putString("xiaoshiCode", "");
            intent02.putExtras(bundle02);
            ErweimaDialog.this.setResult(100, intent02);
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    //监听事件
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.sao:
                startActivityForResult(new Intent(ErweimaDialog.this, CaptureActivity.class), 0);
                break;
            case R.id.make:
                String input = mInput.getText().toString();
                if (input.equals("")) {
                    Toast.makeText(ErweimaDialog.this, "输入不能为空", Toast.LENGTH_LONG).show();
                } else {
                    bitmap = EncodingUtils.createQRCode(input, 500, 500, mLogo.isChecked() ?
                            BitmapFactory.decodeResource(getResources(), R.mipmap.logo) :
                            null);
                    erweima.setImageBitmap(bitmap);
                }

                break;
            case R.id.ok02:

                Bundle bundle = new Bundle();
                Intent intent = new Intent();
                if (erweima.getDrawable() != null) {
//                    mInput.setHint( mInput.getText().toString());
//                    mInput.setEnabled(false);
//                    mInput.setFocusableInTouchMode(false);
//                    mInput.clearFocus();
//
                    bundle.putString("erweimaImage", erweima.getDrawable().toString());
                    bundle.putString("xiaoshiCode", mInput.getText().toString());
                    intent.putExtras(bundle);
                    ErweimaDialog.this.setResult(100, intent);
                } else {
                    bundle.putString("erweimaImage", "");
                    bundle.putString("xiaoshiCode", "");
                    intent.putExtras(bundle);
                    ErweimaDialog.this.setResult(100, intent);
                }


                finish();
                break;
        }

    }

    //二维码返回的结果请求信息
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            String result = bundle.getString("result");
            resultText.setText(result);
            //这里跳转到添加好友界面
        }
    }
}
